﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using umbraco.cms.businesslogic.web;
using umbraco.editorControls;
using CodeGecko.Umbraco.DataTypes.Umb45x;

namespace CodeGecko.Umbraco.DataTypes.Umb45x
{
    public partial class DoctypeDropdown : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        public int umbracoValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                List<DocumentType> d = new List<DocumentType>();
                Helper.GetDocumentTypesByParent(DocumentType.GetByAlias("BlogPost").Id, ref d);
                lstBlogpostDoctypes.DataSource = d;
                lstBlogpostDoctypes.DataTextField = "Text";
                lstBlogpostDoctypes.DataValueField = "ID";
                lstBlogpostDoctypes.DataBind();
                int test;
                if (int.TryParse(value.ToString(), out test)) {
                    int i = 0;
                    foreach (ListItem dl in lstBlogpostDoctypes.Items) {
                        if (dl.Value == test.ToString()) {
                            lstBlogpostDoctypes.SelectedIndex = i;
                            dl.Selected = true;
                            break;
                        }
                        i++;
                    }
                    
                }
            } else {
                // Save data
                value = lstBlogpostDoctypes.SelectedValue;
            }
        }

        #region IUsercontrolDataEditor Members

        public object value {
            get { return umbracoValue; }
            set { if (int.TryParse(value.ToString(), out umbracoValue)) umbracoValue = Convert.ToInt32(value); }
        }

        #endregion
    }
}